package com.ruoyi.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.app.domain.bo.AppDetailBo;
import com.ruoyi.app.domain.vo.AppDetailVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppDetailService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * APP用户账单明细
 *
 * @author heik
 * @date 2023-08-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/detail")
public class AppDetailController extends BaseController {


    private final IAppUserService appUserService;
    private final IAppDetailService iAppDetailService;


    /**
     * 查询APP用户账单明细列表
     */
    @SaCheckPermission("system:detail:list")
    @GetMapping("/list")
    public TableDataInfo<AppDetailVo> list(AppDetailBo bo, PageQuery pageQuery) {
        TableDataInfo<AppDetailVo> page = iAppDetailService.queryPageList(bo, pageQuery);
        page.getData().forEach(e -> {
            AppUserVo appUserVo = appUserService.queryById(e.getUserId());
            if (Objects.nonNull(appUserVo)) {
                e.setUserName(appUserVo.getUserName());
            }
        });
        return page;
    }

    /**
     * 导出APP用户账单明细列表
     */
    @SaCheckPermission("system:detail:export")
    @Log(title = "APP用户账单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppDetailBo bo, HttpServletResponse response) {
        List<AppDetailVo> list = iAppDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP用户账单明细", AppDetailVo.class, response);
    }

    /**
     * 获取APP用户账单明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:detail:query")
    @GetMapping("/{id}")
    public R<AppDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iAppDetailService.queryById(id));
    }
}
